//
//  PhotosViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PhotosViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "PhotoCell.h"
#import "PhotodetailsViewController.h"
#import "NightManager.h"
@interface PhotosViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic, retain)UICollectionView *photoshow;
@end

@implementation PhotosViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.photoArr = [NSMutableArray array];
        self.smallPhotoArr = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc
{
    [_cityID release];
    [_photoArr release];
    [_photoshow release];
    [_smallPhotoArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self netWork];

    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(100, 100);
    
    flowLayout.minimumInteritemSpacing = 10;
    
    flowLayout.minimumLineSpacing = 10;
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.photoshow = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    [flowLayout release];

    self.photoshow.backgroundColor = [UIColor whiteColor];
    self.photoshow.delegate = self;
    self.photoshow.dataSource = self;
    [self.view addSubview:self.photoshow];
    [_photoshow release];
    [self.photoshow registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"reuse"];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotodetailsViewController *pdvc = [[PhotodetailsViewController alloc]init];
    pdvc.pageNum = indexPath.row;
    pdvc.photoBigArr = self.photoArr;

    [self.navigationController pushViewController:pdvc animated:YES];
    [pdvc release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photoArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSURL *url = [NSURL URLWithString:self.smallPhotoArr[indexPath.row]];
    [cell.myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)netWork
{
    NSString *ss = @"%";
    NSString *s = @"1";
    [s integerValue];
    
    NSString *strUrl = [NSString stringWithFormat:@"http://open.qyer.com/place/common/get_photo_list?app_installtime=1429487962&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88337447020836&lon=121.5391037867473&objectid=%@&page=1&pagesize=40&screensize=640&track_app_channel=App%@2520Store&track_app_version=6.2&track_device_info=iPhone%@25204S&track_deviceid=A902EA06-0821-4413-B962-992BC50581A7&track_os=ios%@25207.1.1&type=poi&v=1", self.cityID, ss, ss, ss];
    
    AFHTTPRequestOperationManager *maneger = [AFHTTPRequestOperationManager manager];
    [maneger GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in arr) {
            NSString *url = [dic objectForKey:@"photourl"];
            [self.photoArr addObject:url];
            NSString *small = [dic objectForKey:@"small_photourl"];
            [self.smallPhotoArr addObject:small];
        }
        [self.photoshow reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error=%@", error);
    }];
}

- (void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.photoshow];
}

- (void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.photoshow];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
